<template>
  <Transition name="loading-fade" appear>
    <div v-if="visible" class="loading-overlay">
      <div class="loading-container">
        <div class="spinner">
          <div 
            v-for="i in 12" 
            :key="i" 
            class="spinner-line"
            :style="{ 
              transform: `rotate(${(i - 1) * 30}deg)`,
              '--line-index': i - 1
            }"
          ></div>
        </div>
        <div class="loading-text">加载中</div>
      </div>
    </div>
  </Transition>
</template>

<script setup lang="ts">
interface Props {
  visible: boolean
}

defineProps<Props>()
</script>

<style scoped>
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(2px);
  /* 移动端视口稳定性优化 */
  height: 100vh;
  height: 100dvh; /* 动态视口高度 */
  width: 100vw;
  width: 100dvw; /* 动态视口宽度 */
  /* 确保在移动端滚动时保持固定 */
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

.loading-container {
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
  border-radius: 16px;
  padding: 32px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  min-width: 160px;
  /* 移动端定位稳定性 */
  position: relative;
  z-index: 10000;
}

.spinner {
  position: relative;
  width: 40px;
  height: 40px;
  animation: spinner-rotate 1.2s linear infinite;
}

.spinner-line {
  position: absolute;
  top: 0;
  left: 50%;
  width: 3px;
  height: 12px;
  background: #ffffff;
  border-radius: 2px;
  transform-origin: 50% 20px;
  animation: line-pulse 1.2s ease-in-out infinite;
  animation-delay: calc(var(--line-index) * 0.1s);
}

.loading-text {
  color: #ffffff;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
}

@keyframes spinner-rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes line-pulse {
  0%, 100% {
    opacity: 1;
    transform: scaleY(1);
  }
  50% {
    opacity: 0.3;
    transform: scaleY(0.8);
  }
}

/* 过渡动画 */
.loading-fade-enter-active,
.loading-fade-leave-active {
  transition: opacity 0.3s ease-in-out;
}

.loading-fade-enter-from {
  opacity: 0;
}

.loading-fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .loading-overlay {
    /* 移动端特殊处理 */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    /* 使用更精确的中心定位 */
    display: flex;
    align-items: center;
    justify-content: center;
    /* 防止移动端滚动影响 */
    overflow: hidden;
    /* 移动端中心定位优化 */
    height: 100vh;
    height: 100dvh;
  }
  
  .loading-container {
    padding: 24px 32px;
    min-width: 140px;
    /* 移动端定位优化 */
    position: relative;
    transform: none;
    /* 微调垂直位置，补偿移动端偏移 */
    margin-top: -10px;
  }
  
  .spinner {
    width: 36px;
    height: 36px;
  }
  
  .spinner-line {
    width: 2.5px;
    height: 10px;
  }
  
  .loading-text {
    font-size: 14px;
  }
}

/* 移动端Safari特殊处理 */
@supports (-webkit-touch-callout: none) {
  .loading-overlay {
    /* Safari移动端优化 */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 100vh;
    height: 100dvh;
  }
}

/* 超小屏幕设备优化 */
@media (max-width: 480px) {
  .loading-overlay {
    /* 超小屏幕使用更精确的定位 */
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    height: 100dvh;
  }
  
  .loading-container {
    /* 超小屏幕微调 */
    margin-top: -15px;
    padding: 20px 28px;
    min-width: 120px;
  }
  
  .spinner {
    width: 32px;
    height: 32px;
  }
  
  .spinner-line {
    width: 2px;
    height: 8px;
  }
  
  .loading-text {
    font-size: 13px;
  }
}
</style>
